package com.zhenyi.fenda.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fenda.common.model.FdAnswer;
import com.fenda.common.model.FdConsult;
import com.fenda.common.model.FdMaster;
import com.fenda.common.model.FdNotification;
import com.fenda.common.model.FdOrder;
import com.fenda.common.model.FdOrderComment;
import com.fenda.common.model.FdQuestion;
import com.fenda.common.model.FdUser;
import com.jfinal.aop.Clear;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.jfinal.weixin.sdk.api.SnsApi;
import com.jfinal.weixin.sdk.kit.IpKit;
import com.utils.AuthUtils;
import com.zhenyi.fenda.core.WXPayUtils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class HomeController extends FDController {

	private WXPayUtils payUtils = new WXPayUtils("wx24524e8b092948cf", "1341307301", "quickplainquickplainquickplainJW",
			"http://app.cenjiawen.com/fd/bill/billPush");

	protected String getBodyParam() {
		return HttpKit.readData(getRequest());
	}

	@Clear
	public void debug() {
		Long userId = getParaToLong("userId", 10L);
		FdUser tmp = FdUser.dao.findById(userId);
		if (tmp == null) {
			renderFailure();
			return;
		}
		String uid = String.valueOf(userId);
		String token = AuthUtils.login(uid, this);
		FdUser user = tmp.userInfo();
		renderSuccess(user);
		String state = getPara("STATE");
		if (!StrKit.isBlank(state)) {
			redirect(state);
		} else {
			redirect("/fd");
		}
	}

	public void logout() {
		AuthUtils.logout(this);
		redirect("/fd/");
	}

	@Clear
	public void auth() {
		String code = getPara("code");
		if (StrKit.isBlank(code)) {
			renderError(404);
			return;
		}
		SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(APP_ID, APP_SECRET, code);
		if (!snsAccessToken.isAvailable()) {
			LogKit.error("sns access token fail");
			renderFailure();
			return;
		}
		String openId = snsAccessToken.getOpenid();
		String accessToken = snsAccessToken.getAccessToken();
		ApiResult snsApi = SnsApi.getUserInfo(accessToken, openId);
		if (!snsApi.isSucceed()) {
			System.out.println(snsApi.getJson());
			System.out.println("" + IpKit.getRealIpV2(getRequest()));
			renderFailure();
			return;
		}
		FdUser user = FdUser.dao.searchFirst("openId", openId);
		if (user == null) {
			user = new FdUser();
			user.setOpenId(openId);
			user.save();
			user.setUid(hashId.encode(user.getId()));
			user.setCode(hashId.encode(user.getId()));
		}

		user.setUid(hashId.encode(user.getId()));
		user.setCode(hashId.encode(user.getId()));
		user.setName(snsApi.getStr("nickname"));
		user.setLogo(snsApi.getStr("headimgurl"));
		user.update();
		String state = getPara("state");
		Long userId = user.getId();
		String uid = String.valueOf(userId);
		String token = AuthUtils.login(uid, this);
		if (!StrKit.isBlank(state)) {
			redirect(state);
		} else {
			redirect("/fd");
		}

	}

	/**
	 * 生成订单
	 * 
	 * @param master
	 * @param userId
	 * @return
	 * @Time 2018年7月19日 下午9:43:06
	 */
	private FdOrder genOrder(Integer price, FdUser user) {
		Long userId = user.getId();
		String openId = user.getOpenId();
		FdOrder order = new FdOrder();
		order.setUserId(userId);
		order.setPrice(price);
		order.setTotalFee(String.valueOf(price));
		order.setOpenId(openId);
		order.save();
		Long orderId = order.getId();
		String no = DateUtil.format(new Date(), "YYYYMMddhhmmss") + orderHash.encode(orderId);
		order.setOrderNo(no);
		order.setOutTradeNo(no);
		order.update();
		return order;
	}

	@Override
	public void index() {
		super.index();
		// HttpServletRequest request = getRequest();
		// Enumeration<String> header = request.getHeaderNames();
		// while (header.hasMoreElements()) {
		// String string = (String) header.nextElement();
		// String value = request.getHeader(string);
		// System.out.println(string + " - " + value);
		// }
		// String host = getRequest().getHeader("Host");
		// System.out.println(host);
		SqlPara commentSqlPara = Db.getSqlPara("FdMasterConsultComment");
		Page<Record> pages = Db.paginate(getPage(), 20, commentSqlPara);
		setItems(pages.getList());
		SqlPara consultSql = Db.getSqlPara("FDFindMasterConsultRecord");
		List<Record> consults = Db.paginate(1, 3, consultSql).getList();
		setAttr("consults", consults);

		Cache cache = Redis.use();
		int answerCount = 0;
		if (cache.exists("answerCount")) {
			answerCount = cache.get("answerCount");
		} else {
			answerCount = RandomUtil.randomInt(0, 200);
			Date now = DateUtil.parse(DateUtil.now());
			long expireTime = DateUtil.between(now, DateUtil.endOfDay(now), DateUnit.SECOND);
			cache.setex("answerCount", new Long(expireTime).intValue(), answerCount);
		}
		setAttr("answerCount", answerCount);

		render("/fd/index.html");
	}

	public void consultList() {
		FdUser user = getUser();
		Kv cond = Kv.by("time", new Date());
		cond.set("userId", user.getId());
		SqlPara sqlPara = Db.getSqlPara("FDFindConsultList", cond);
		List<Record> items = Db.find(sqlPara);
		setItems(items);
		print(items);
		renderSuccess(items);
		render("/fd/consultList.html");
	}

	public void userInfo() {
		FdUser user = getUser();
		setInfoItem(user);
		render("/fd/userInfo.html");
	}

	public void masterList() {
		SqlPara sqlPara = Db.getSqlPara("FDFindMasters");
		Page<Record> pages = Db.paginate(getPage(), pageSize, sqlPara);
		setItems(pages.getList());
		render("/fd/masterList.html");
	}

	public void masterHome() {
		Kv kv = Kv.create();
		Integer userId = getParaToInt("userId");
		if (userId == null) {
			renderFailure();
			return;
		}
		kv.set("userId", userId);
		SqlPara sqlPara = Db.getSqlPara("FDFindMasters", kv);
		FdMaster master = FdMaster.dao.findFirst(sqlPara);
		master.setVisitCount(master.getVisitCount() + 1);
		master.update();

		SqlPara tagSql = Db.getSqlPara("FindMasterTag", master.getUserId());
		List<Record> tags = Db.find(tagSql);
		if (tags.size() > 0) {
			master.put("tags", tags);
		}
		setInfoItem(master);
		setAttr("master", master);
		Calendar cal = Calendar.getInstance();
		setAttr("workTime", cal.get(Calendar.YEAR) - master.getJoinYears());
		SqlPara commentSqlPara = Db.getSqlPara("FdMasterConsultComment", kv);
		Page<Record> pages = Db.paginate(1, 10, commentSqlPara);
		setAttr("pages", pages);

		render("/fd/masterHome.html");
	}

	/**
	 * 提交问题
	 */
	public void submitQuestion() {
		FdUser user = getUser();
		if (user == null) {
			renderFailure(500, "error user");
			return;
		}
		String body = HttpKit.readData(getRequest());
		JSONObject jsonObject = JSONUtil.parseObj(body);
		Long masterId = jsonObject.getLong("masterId");
		Integer price = jsonObject.getInt("price");
		String questionContent = jsonObject.getStr("questionContent");
		if (StrKit.isBlank(questionContent)) {
			renderFailure(500, "提交内容为空");
			return;
		}
		Long userId = user.getId();

		if (masterId != null) {
			FdMaster master = FdMaster.dao.findById(masterId);
			if (master == null) {
				renderFailure(500, "error master");
				return;
			}
			price = master.getPrice();
		} else if (price == null) {
			renderFailure(500, "price master");
			return;
		}

		String content = questionContent;
		String title = getPara("title", "");
		FdOrder order = genOrder(price, user);
		if (order == null) {
			renderFailure(500, "error order");
			return;
		}
		Long orderId = order.getId();
		FdQuestion fdQuestion = new FdQuestion();
		fdQuestion.setTitle(title);
		fdQuestion.setContent(content);
		fdQuestion.setUserId(userId);
		fdQuestion.setOrderId(orderId);
		fdQuestion.setMasterId(masterId);
		fdQuestion.save();
		fdQuestion.setCode(hashId.encode(fdQuestion.getId()));
		fdQuestion.update();
		renderSuccess(order.getOrderNo());
	}

	/**
	 * 我的订单列表
	 */
	public void orderList() {
		Long userId = getUser().getId();
		Kv kv = Kv.by("userId", userId);
		Integer status = getParaToInt("status", 0);
		if (status != null) {
			kv.set("status", status);
		}
		kv.set("consult", 1);
		SqlPara sqlPara = Db.getSqlPara("FdFindOrders", kv);
		List<Record> items = Db.find(sqlPara);
		setItems(items);
		render("/fd/orderList.html");
	}

	/**
	 * 订单详情
	 */
	// public void orderInfo() {
	// String orderNo = getPara(0);
	// FdOrder order = FdOrder.dao.searchFirst("orderNo", orderNo);
	// render("/fd/orderConfirm.html");
	// }

	/**
	 * 问题列表
	 */
	public void questionList() {
		Integer type = getParaToInt("type");
		SqlPara sqlPara = null;
		if (type == null) {
			sqlPara = Db.getSqlPara("FdFindAnsweredQuestions");
		} else {
			Kv cond = Kv.by("type", type);
			sqlPara = Db.getSqlPara("FdFindAnsweredQuestions", cond);
			setAttr("type", type);
		}
		Page<Record> items = Db.paginate(getPage(), pageSize, sqlPara);
		setItems(items.getList());

		SqlPara sqlPara2 = Db.getSqlPara("FDFindQuestionNum");
		Record record = Db.findFirst(sqlPara2);
		setAttr("questionNum", record.getLong("num"));// 问题数量

		render("/fd/questionList.html");
	}

	/**
	 * 问题详情
	 */
	public void questionInfo() {
		super.questionInfo();
	}

	public void questStep1() {
		render("/fd/questStep1.html");
	}

	public void questStep2() {
		SqlPara sqlPara = Db.getSqlPara("FDFindMasters");
		Page<Record> pages = Db.paginate(getPage(), pageSize, sqlPara);
		setItems(pages.getList());
		render("/fd/questStep2.html");
	}

	/**
	 * 提交咨询信息
	 */
	public void submitConsult() {
		String body = HttpKit.readData(getRequest());
		JSONObject jsonObject = JSONUtil.parseObj(body);
		Long masterId = jsonObject.getLong("masterId");
		FdUser user = getUser();
		FdMaster master = FdMaster.dao.findById(masterId);
		if (master == null) {
			renderMsg("master error");
			return;
		}
		FdOrder order = genOrder(master.getPrice(), user);
		if (order == null) {
			renderFailure(500, "error order");
			return;
		}
		Long orderId = order.getId();
		FdConsult consult = new FdConsult();
		consult.setContent("简单咨询");
		consult.setMasterId(masterId);
		consult.setOrderId(orderId);
		consult.save();
		renderSuccess(order.getOrderNo());
	}

	public void payConsult() {
		String orderNo = getPara(0);
		FdOrder order = FdOrder.dao.searchFirst("orderNo", orderNo);
		if (order == null) {
			renderFailure();
			return;
		}
		FdUser user = getUser();
		if (!user.getId().equals(order.getUserId())) {
			renderFailure();
			return;
		}
		FdConsult consult = FdConsult.dao.searchFirst("orderId", order.getId());
		if (consult == null) {
			renderFailure();
			return;
		}
		setAttr("content", "在线咨询老师");
		setAttr("order", order);
		render("/fd/orderConfirm.html");
	}

	public void payQuestion() {
		// String orderNo = getPara("orderNo");
		// FdOrder order = FdOrder.dao.searchFirst("orderNo", orderNo);
		// if (order == null) {
		// renderFailure();
		// return;
		// }
		// FdUser user = getUser();
		// if (!user.getId().equals(order.getUserId())) {
		// renderFailure();
		// return;
		// }
		// FdQuestion question = FdQuestion.dao.searchFirst("orderId",
		// order.getId());
		// if (question == null) {
		// renderFailure();
		// return;
		// }
		// setAttr("content", question.getContent());
		// setAttr("order", order);
		// setAttr("question", question);
		// render("/fd/orderConfirm.html");
		pay();
	}

	/**
	 * 支付页面
	 */
	public void pay() {
		String orderNo = getPara("orderNo");
		FdOrder order = FdOrder.dao.searchFirst("orderNo", orderNo);
		if (order == null) {
			renderFailure();
			return;
		}
		FdUser user = getUser();
		if (!user.getId().equals(order.getUserId())) {
			renderFailure();
			return;
		}
		render("/fd/orderConfirm.html");
		String openId = order.getOpenId();
		String outTradeNo = order.getOutTradeNo();
		String total = order.getTotalFee();
		if (DEBUG) {
			total = "1";
		}
		String prepayId = order.getPrePayId();
		String ip = "127.0.0.1";
		// ip = IpKit.getRealIp(getRequest());
		Ret ret = payUtils.pushOrder(outTradeNo, total, ip, openId, prepayId);
		if (ret.isFail()) {
			renderFailure();
			return;
		}
		@SuppressWarnings("unchecked")
		Map<String, String> packageParams = (Map<String, String>) ret.get("packageParams");
		if (StrKit.isBlank(order.getPrePayId())) {
			order.setPrePayId(packageParams.get("package"));
		}
		order.update();
		System.out.println(JsonKit.toJson(packageParams));
		setAttr("packageParams", packageParams);
		FdQuestion question = FdQuestion.dao.searchFirst("orderId", order.getId());
		if (question != null) {
			setAttr("content", question.getContent());
			setAttr("order", order);
			setAttr("question", question);
			return;
		}
		FdConsult consult = FdConsult.dao.searchFirst("orderId", order.getId());
		if (consult != null) {
			setAttr("content", "在线咨询老师");
			setAttr("order", order);
			return;
		}
		// PaymentKit.createSign(params, paternerKey)
		renderFailure();
	}

	// private FdRoom createChatRoom() {
	// FdRoom room = new FdRoom();
	// // room.setUserId(userId);
	// // room.setMasterId(masterId);
	// room.setExpire(new DateTime().plusMinutes(60).toDate());
	// room.save();
	// room.setCode(hashId.encode(room.getId()));
	// room.update();
	// return room;
	// }

	/**
	 * 假支付
	 */
	// @Before(TestInterceptor.class)
	// public void fakePay() {
	// String body = getBodyParam();
	// JSONObject jsonObject = JSONUtil.parseObj(body);
	// String orderNo = jsonObject.getStr("orderNo");// getPara("orderNo");
	// FdOrder order = FdOrder.dao.searchFirst("orderNo", orderNo);
	// FdConsult consult = FdConsult.dao.searchFirst("orderId", order.getId());
	// if (consult != null) {
	// if (consult.getRoomId() == null) {
	// consult.print();
	// FdRoom room = createChatRoom();
	// room.setUserId(order.getUserId());
	// room.setMasterId(consult.getMasterId());
	// room.update();
	// consult.setRoomId(room.getId());
	// consult.update();
	// }
	// Long mastetId = consult.getMasterId();
	// FdMaster master = FdMaster.dao.findById(mastetId);
	// master.print();
	// master.setPaidCount(master.getPaidCount() + 1);
	// master.print();
	// master.update();
	// }
	// FdQuestion question = FdQuestion.dao.searchFirst("orderId",
	// order.getId());
	// if (question != null) {
	// }
	// order.setStatus(1);
	// order.update();
	// // redirect("/fd/orderStatus?orderNo=" + orderNo);
	// renderSuccess();
	// }

	/**
	 * 订单状态
	 */
	public void orderStatus() {
		String orderNo = getPara("orderNo");
		FdOrder order = FdOrder.dao.searchFirst("orderNo", orderNo);
		if (order == null) {
			renderFailure();
			return;
		}
		setAttr("order", order);
		render("/fd/orderStatus.html");
	}

	// public void orderConfirm() {
	// render("/fd/orderConfirm.html");
	// }

	/**
	 * 用户回答问题
	 */
	public void fakeAnswer() {
		Long questionId = getParaToLong("questionId");
		FdUser user = getUser();
		Long userId = user.getId();
		FdAnswer answer = new FdAnswer();
		answer.setContent("回答下这个问题" + new Date());
		answer.setQuestionId(questionId);
		answer.setUserId(userId);
		answer.save();
		renderSuccess();
	}

	/**
	 * 我的问题
	 */
	public void userQuestions() {
		Long userId = getUser().getId();
		Kv kv = Kv.by("userId", userId);
		Integer status = getParaToInt("status");
		if (status != null) {
			kv.set("status", status);
		}
		SqlPara sqlPara = Db.getSqlPara("FdFindQuestion", kv);
		List<Record> items = Db.paginate(getPage(), pageSize, sqlPara).getList();
		renderSuccess(items);
	}

	public void userNotAnswerQuestions() {
		FdUser user = getUser();
		SqlPara sqlPara = Db.getSqlPara("FdUserNotAnswerQuestion", user.getId());
		List<Record> items = Db.find(sqlPara);
		setItems(items);
		render("/fd/userNotAnswerQuestions.html");
	}

	/**
	 * 用户去回答问题
	 */
	public void answerQuestion() {
		String code = getPara(0);
		setAttr("code", code);
		Long questionId = hashId.decode(code)[0];
		Kv kv = Kv.by("questionId", questionId);
		SqlPara sqlPara = Db.getSqlPara("FdFindQuestion", kv);
		Record record = Db.findFirst(sqlPara);
		print(record);
		setInfoItem(record);
		render("/fd/answer.html");
	}

	/**
	 * 用户去回答问题
	 */
	public void answer() {
		String body = getBodyParam();
		JSONObject jsonObject = new JSONObject(body);
		String code = jsonObject.getStr("code");// getBodyPara("code");
		Long questionId = hashId.decode(code)[0];
		String content = jsonObject.getStr("content");
		if (StrKit.isBlank(content)) {
			renderFailure(500, "提交内容为空");
			return;
		}
		FdUser user = getUser();
		Long userId = user.getId();
		FdAnswer answer = new FdAnswer();
		answer.setUserId(userId);
		answer.setContent(content);
		answer.setQuestionId(questionId);
		answer.save();

		FdQuestion question = FdQuestion.dao.findById(questionId);
		// 消息提醒
		FdNotification fdNotification = new FdNotification();
		fdNotification.setUserId(question.getUserId());
		fdNotification.setType(NOTIFICACATION_QUESTION);
		fdNotification.setQuestionId(questionId);
		fdNotification.setAddTime(new Date());
		fdNotification.setContent("问题回复提醒");
		fdNotification.save();
		HashMap<String, Object> map = new HashMap<>();
		map.put("code", code);
		renderResult(map, "ok", 200);
		// renderSuccess();
	}

	/**
	 * 未评论的订单
	 * 
	 * @Time 2018年7月21日 下午7:59:31
	 */
	public void notCommentOrders() {
		Integer type = getParaToInt("type");
		FdUser user = getUser();
		Kv cond = Kv.by("type", type);
		cond.set("userId", user.getId());
		SqlPara sqlPara = Db.getSqlPara("FdFindNotCommentOrders", cond);
		List<Record> items = Db.find(sqlPara);
		setItems(items);
		setAttr("items", items);
		System.out.println(items);
		render("/fd/notCommentOrders.html");
	}

	/**
	 * 房间聊天记录
	 */
	public void roomMessage() {
		Integer pageNumber = getParaToInt("pageNumber", 1);
		String code = getPara("code");
		FdUser user = getUser();
		System.out.println(code);
		Long roomId = hashId.decode(code)[0];
		System.out.println(roomId);
		// 聊天记录
		Kv cond = Kv.by("roomId", roomId);
		SqlPara sqlPara = Db.getSqlPara("FDFindMessageInRoom", cond);
		Page<Record> list = Db.paginate(pageNumber, pageSize, sqlPara);
		setAttr("list", list.getList());
		setAttr("user", user);
		render("/fd/master/roomInfo.html");
	}

	/**
	 * 未评论的订单详情
	 */
	public void notCommentOrderInfo() {
		Long questionId = getParaToLong("questionId");
		Long consultId = getParaToLong("consultId");
		Record record = null;
		if (questionId != null) {
			Kv cond = Kv.by("questionId", questionId);
			SqlPara sqlPara = Db.getSqlPara("FdFindNotCommentOrderInfo", cond);
			record = Db.findFirst(sqlPara);
			FdAnswer fdAnswer = FdAnswer.dao.findById(record.getLong("answerId"));
			record.set("answerContent", fdAnswer.getContent());
			record.set("answerAddTime", fdAnswer.getAddTime());
		} else if (consultId != null) {
			Kv cond = Kv.by("consultId", consultId);
			SqlPara sqlPara = Db.getSqlPara("FdFindNotCommentOrderInfo", cond);
			record = Db.findFirst(sqlPara);
		}
		setAttr("record", record);
		render("/fd/commentOrder.html");
	}

	/**
	 * 已完成的订单
	 */
	public void completeOrder() {
		FdUser user = getUser();
		SqlPara sqlPara = Db.getSqlPara("findCompleteOrder", user.getId());
		List<Record> items = Db.find(sqlPara);
		setItems(items);
		render("/fd/completeOrder.html");
	}

	/**
	 * 评论订单
	 */
	public void fakeCommentOrder() {
		String json = HttpKit.readData(getRequest());
		JSONObject jsonObject = new JSONObject(json);
		String orderNo = jsonObject.getStr("orderNo");
		String content = jsonObject.getStr("content", "大师真棒");
		FdOrder order = FdOrder.dao.searchFirst("orderNo", orderNo);
		FdOrderComment orderComment = new FdOrderComment();
		orderComment.setQuality(jsonObject.getInt("quality"));// 咨询质量
		orderComment.setAttitude(jsonObject.getInt("attitude"));// 服务态度
		orderComment.setReplySpeed(jsonObject.getInt("replySpeed"));// 回复速度
		orderComment.setContent(content);
		orderComment.setOrderId(order.getId());
		orderComment.save();

		Long masterId = null;
		int quality = 50;
		int attitude = 50;
		int replySpeed = 50;

		System.out.println(jsonObject.getLong("questionId"));
		System.out.println(jsonObject.getLong("consultId"));
		if (jsonObject.getLong("questionId") != null) {
			FdQuestion fdQuestion = FdQuestion.dao.findById(jsonObject.getLong("questionId"));
			masterId = fdQuestion.getMasterId();
		} else {
			FdConsult fdConsult = FdConsult.dao.findById(jsonObject.getLong("consultId"));
			masterId = fdConsult.getMasterId();
		}

		Kv cond = Kv.by("masterId", masterId);
		SqlPara sqlPara2 = Db.getSqlPara("findComment", cond);
		Record record = Db.findFirst(sqlPara2);
		quality = (int) Math.floor(record.getBigDecimal("quality").doubleValue() * 10);
		attitude = (int) Math.floor(record.getBigDecimal("attitude").doubleValue() * 10);
		replySpeed = (int) Math.floor(record.getBigDecimal("replySpeed").doubleValue() * 10);

		FdMaster fdMaster = FdMaster.dao.findById(masterId);
		fdMaster.setCommentCount(fdMaster.getCommentCount() + 1);// 更新评论数
		fdMaster.setQuality(quality);
		fdMaster.setAttitude(attitude);
		fdMaster.setReplySpeed(replySpeed);
		double rate = (quality + attitude + replySpeed) / 30.0;
		fdMaster.setRate(String.format("%.1f", rate));
		fdMaster.update();
		renderSuccess();
	}

	/**
	 * 消息提醒列表
	 */
	public void notification() {
		Integer pageNumber = getParaToInt("pageNumber", 1);
		FdUser user = getUser();
		Kv cond = Kv.by("userId", user.getId());
		// 删除过期的消息
		// SqlPara sqlPara1 = Db.getSqlPara("findOutTimeNotification", cond);
		// List<Record> list1 = Db.find(sqlPara1);
		// for (Record record : list1) {
		// FdNotification.dao.deleteById(record.getLong("id"));// 删除过期的消息
		// }
		SqlPara sqlPara = Db.getSqlPara("findNotification", cond);
		Page<Record> list = Db.paginate(pageNumber, pageSize, sqlPara);
		setAttr("list", list);
		System.out.println(list.getList());
		render("/fd/notificationList.html");
	}

	/**
	 * 清空信息提醒
	 */
	public void emptyNotification() {
		FdUser user = getUser();
		Kv cond = Kv.by("userId", user.getId());
		SqlPara sqlPara = Db.getSqlPara("findUserNotification", cond);
		List<Record> list = Db.find(sqlPara);
		for (Record record : list) {
			FdNotification.dao.deleteById(record.getLong("id"));
		}
		redirect("/fd/notification");
	}

	public void socket() {
		render("/fd/socket2.html");
		try {
			Thread.sleep(1000000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		renderSuccess();
	}

	/**
	 * ------------------教师端
	 */
}
